package com.moliku.insurance.common.validation;


import com.moliku.insurance.common.exception.ServiceException;
import com.moliku.insurance.common.function.ExceptionExecutor;
import com.moliku.insurance.common.response.ResponseCode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * 业务断言执行器
 *
 * @author Mr.Liu
 * @since 2020-01-02
 */
public class BusinessAssert {
    private BusinessAssert() {
    }

    public static void isNotNull(Object object, String errorMessage) {
        if (object == null) {
            throw new ServiceException(errorMessage);
        }
    }

    public static <T extends RuntimeException> void isNotNull(Object object, ExceptionExecutor<T> executor) {
        if (object == null) {
            throw executor.getException();
        }
    }

    public static void isFalse(boolean expression, String errorMessage) {
        if (expression) {
            throw new ServiceException(errorMessage);
        }
    }

    public static void isTrue(boolean expression, String errorMessage) {
        isTrue(expression, () -> new ServiceException(errorMessage));
    }

    public static void isTrue(boolean expression, ResponseCode businessStatus) {
        isTrue(expression, () -> new ServiceException(businessStatus));
    }

    public static <T extends RuntimeException> void isTrue(boolean expression, ExceptionExecutor<T> executor) {
        if (!expression) {
            throw executor.getException();
        }
    }

    public static void isNull(Object object, String errorMessage) {
        isNull(object, () -> new ServiceException(errorMessage));
    }

    public static <T extends RuntimeException> void isNull(Object object, ExceptionExecutor<T> executor) {
        if (object != null) {
            throw executor.getException();
        }
    }

    public static void isNotBlank(String string, String errorMessage) {
        isNotBlank(string, () -> new ServiceException(errorMessage));
    }

    public static void isNotBlank(String string, ResponseCode businessStatus) {
        isNotBlank(string, () -> new ServiceException(businessStatus));
    }

    public static <T extends RuntimeException> void isNotBlank(String string, ExceptionExecutor<T> executor) {
        if (StringUtils.isBlank(string)) {
            throw executor.getException();
        }
    }

    public static void isEmpty(Collection<?> coll, String errorMessage) {
        isEmpty(coll, () -> new ServiceException(errorMessage));
    }

    public static <T extends RuntimeException> void isEmpty(Collection<?> coll, ExceptionExecutor<T> executor) {
        if (CollectionUtils.isNotEmpty(coll)) {
            throw executor.getException();
        }
    }

//
//    public static void isNotEmpty(@Nullable Object[] obj, String errorMessage) {
//        executor(obj == null || obj.length == 0, () -> new ServiceException(errorMessage));
//    }
//
//    public static void isNotEmpty(@Nullable Collection<?> coll, String errorMessage) {
//        isNotEmpty(coll, () -> new ServiceException(errorMessage));
//    }
//
//    public static <T extends RuntimeException> void isNotEmpty(@Nullable Collection<?> coll, ExceptionExecutor<T> executor) {
//        executor(CollectionUtils.isNotEmpty(coll), executor);
//    }
//

//
//    public static void isBlank(@Nullable String string, String errorMessage) {
//        isBlank(string, () -> new ServiceException(errorMessage));
//    }
//
//    public static void isBlank(@Nullable String string, ResponseCode businessStatus) {
//        isBlank(string, () -> new ServiceException(businessStatus));
//    }
//
//    public static <T extends RuntimeException> void isBlank(@Nullable String string, ExceptionExecutor<T> executor) {
//        executor(StringUtils.isBlank(string), executor);
//    }
//
//    public static void isNotNull(@Nullable Object object, String errorMessage) {
//        executor(object != null, () -> new ServiceException(errorMessage));
//    }
//
//    public static void isNotNull(@Nullable Object object, ResponseCode businessStatus) {
//        executor(object != null, () -> new ServiceException(businessStatus));
//    }
//
//    public static <T extends RuntimeException> void isNotNull(@Nullable Object object, ExceptionExecutor<T> executor) {
//        executor(object != null, executor);
//    }
//

//

    /**
     * 则抛出自定义异常
     *
     * @param executor 异常执行器
     * @param <T>      T
     */
//    public static <T extends RuntimeException> void executor(ExceptionExecutor<T> executor) {
//        throw executor.getException();
//    }

    /**
     * 无参无返回值执行器
     *
     * @param executor 执行器
     * @param <T>      T
     */
//    public static <T> void executor(ConsumerExecutor executor) {
//        executor.accept();
//    }
}
